package com.health.pay.controller;

import com.health.pay.service.WXOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by fangjicai on 2020/3/24.
 */
@Controller
public class WXTradeController {
    @Autowired
    private WXOrderService WXOrderService;

    /**
     * 生成与支付url，返还到前台调用qrcode js工具类生成二维码
     * @param orderId
     * @return
     */
    @GetMapping("url/{id}")
    public ModelAndView createPayUrl(@PathVariable("id") Long orderId){
        ModelAndView showPayCode = new ModelAndView("showPayCode");
        //根据订单id去数据库查询订单对应金额（略） money=100  对于金额：后台数据库字段格式decimal
        //this.orderService.findById(orderID).getMoney()--->订单金额100--》java --->bigdecimal
        Long money=100L;
        //获取订单描述
        String desc="购买......";
        String url = this.WXOrderService.createOrder(orderId, money, desc);
        showPayCode.addObject("code_url",url);
        showPayCode.addObject("order_id",orderId);
        return showPayCode;
    }

    /**
     * 前台定时循环该接口查询订单状态
     * @param orederId
     * @return
     */
    @GetMapping("state/{id}")
    public int queryOrderState(@PathVariable("id")Long orederId){

        int value = WXOrderService.queryPayState(orederId).getValue();


        return value;
    }

}
